package com.papercutting.platform.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 文件访问配置
 */
@Configuration
public class FileConfig implements WebMvcConfigurer {

    @Value("${app.upload.path:/uploads/}")
    private String uploadPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置上传文件的访问路径
        String baseDir = System.getProperty("user.dir");
        String uploadDir;
        
        if (baseDir.endsWith("backend")) {
            // 如果当前目录已经是backend，直接使用配置路径
            uploadDir = baseDir + "/" + uploadPath;
        } else {
            // 如果当前目录是项目根目录，添加backend前缀
            uploadDir = baseDir + "/backend/" + uploadPath;
        }
        
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:" + uploadDir);

        // 添加日志输出，便于调试
        System.out.println("配置文件访问路径: /uploads/** -> " + uploadDir);
    }
}